<?php
namespace App\Http\Controllers\Member;


use Illuminate\Http\Request;
use App\Models\Order;
class RechargeController extends MemberController
{
    protected $nav;         //当前焦点
    public function __construct(){
        parent::__construct();
        $this->nav=5;
    }
    /**
     * 充值页面
     */
    public function getIndex()
    {
        return view('member.recharge.recharge')
                  ->with('nav',$this->nav);
    }

    /**
     * 选择支付页面
     */
    public function postEbank(Request $req)
    {
        $data = $req->only('fixed_price','order_price');
        if(empty($data['order_price']) && empty($data['fixed_price']) )
            return $this->formatJson('error','请选择金额！');
        if($data['fixed_price'])
            $order_price = $data['fixed_price'];
        elseif($data['order_price'] && !$data['fixed_price'])
            $order_price = $data['fixed_price'];
        $goods_name = '积分充值';
        $goods_description = '用户积分充值，1元可充值10积分。单次最高充值金额为5000';
        return view('member.recharge.bank')->with('nav',$this->nav)->with('order_price',$order_price)
                ->with('googs_name',$goods_name)->with('goods_description',$goods_description);
    }
    
    /**
     * 跳转至支付页面 
     */
    public function postTrade(Request $req)
    {
        if (!str_contains(ALI_NOTIFY, 'cimo') || empty(ALI_NOTIFY)) {
            return $this->formatJson('error','域名不合法');
        }
        $data = $req->only('fixed_price','order_price','goods_name','goods_description','payment_type');
        $highPrice = 5000;
        $order_price = sprintf("%.1f", floatval($req->input('order_price',0)) ); //保留两位小数点
        $goods_name = $data['goods_name'];
        $goods_description = $data['goods_description'];
        $payment_type = $data['payment_type'];
        if(empty($data['order_price']) && empty($data['fixed_price']) )
        {
            return $this->formatJson('error','请选择金额！');
        }
        if($data['fixed_price'])
        {
            $order_price = $data['fixed_price'];
        }   
        if($data['order_price'] && $data['order_price'] > 0)
        {
            $order_price = $data['order_price'];
        }
        $order_id = date("YmdHis",time())."00".$this->uid.get_rnd_num(2);
        if(bccomp($order_price,$highPrice,3) == 1)
        {
            return $this->formatJson('error','超过单次充值金额限制［'.$highPrice.']');
        }
        $goods_name = $goods_name?:'积分充值';
        $goods_description = $goods_description?:'用户积分充值，1元冲10积分，积分可用于用户学习平台教程及文档';
        $order_new = array(
            'order_sn'      => $order_id,
            'price'         => $order_price,
            'goods_name'    => $goods_name,
            'goods_description' => $goods_description,
            'status'        => 0,
            'order_type'    => 0,
            'pay_type'      => $payment_type,
            'user_id'       => $this->uid
        );
        if($payment_type == 'alipay')
        {
            $alipay = app('alipay.web');
            $alipay->setOutTradeNo($order_id);
            $alipay->setTotalFee($order_price);
            $alipay->setSubject($goods_name);
            $alipay->setBody($goods_description);
            Order::firstOrCreate($order_new);
            return redirect()->to($alipay->getPayLink());
        }
    }

    /**
     * 支付成功
     */
    public function getSuccess()
    {

        return view('member.recharge.success')
            ->with('nav',$this->nav);
    }


    /**
     * 支付失败
     */
    public function getFail()
    {

        return view('member.recharge.fail')
            ->with('nav',$this->nav);
    }


}
